<template>
  <div class="order-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <el-table
        ref="singleTable"
        :data="orderList"
        border
        style="width: 100%">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column
          property="order_number"
          label="订单编号"
          width="250">
        </el-table-column>
        <el-table-column
          property="order_price"
          label="订单价格">
        </el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.order_pay === '1'" type="primary">已付款</el-tag>
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          property="is_send"
          label="是否发货">
        </el-table-column>
        <el-table-column
          label="下单时间"
          width="200">
          <template slot-scope="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="addressVisible = true" size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="showExpress" size="mini" type="success" icon="el-icon-location"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <el-dialog
      title="提示"
      :visible.sync="addressVisible"
      width="50%">
      <el-form :model="addressRuleForm" :rules="addressRules" ref="addRuleFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="省市级/县" prop="address">
          <el-cascader :options="cityData" :show-all-levels="false"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detail">
          <el-input v-model="addressRuleForm.detail"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressCancel">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="商品物流"
      :visible.sync="expressVisible"
      width="50%">
      <el-timeline :reverse="true">
        <el-timeline-item
          v-for="(activity, index) in expressList"
          :key="index"
          type="danger"
          placement="top"
          icon="el-icon-time"
          :timestamp="activity.ftime">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="expressVisible = false">取 消</el-button>
        <el-button type="primary" @click="expressVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata.js'
export default {
  data() {
    return {
      expressVisible: false,
      addressVisible: false,
      orderList: [],
      total: 0,
      params: {
        query: '',
        pagesize: 10,
        pagenum: 1
      },
      addressRuleForm: {
        address: '',
        detail: ''
      },
      addressRules: {
        address: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        detail: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      cityData,
      expressList: []
    }
  },
  created(){
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.params
      })

      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.orderList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.params.pagesize = val
      this.getOrderList()
    },
    handleCurrentChange(val) {
      this.params.pagenum = val
      this.getOrderList()
    },
    addressCancel() {
      this.$refs.addRuleFormRef.resetFields()
      this.addressVisible = false
    },
    async showExpress() {
      this.expressVisible = true

      const { data: res } = await this.$http.get('kuaidi/1106975712662')

      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.expressList = res.data
    }
  }
}
</script>
<style lang="less" scoped>
  .order-container{
    .el-card{
      margin-top: 20px;
    }
    .el-table{
      margin: 20px 0;
    }
  }
</style>